﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PessModel;

namespace PessMvc.Controllers
{
    [Authorize(Roles = "Admin")]
    public class StandardController : Controller
    {
        // GET: Standard
        PESSEntities db;
        public StandardController()
        {
            db = new PESSEntities();
        }
                
        public ActionResult Index(int activityID)
        {
            var activity = db.Activities.Find(activityID);
            ViewBag.ActivityID = activityID;
            return View(activity.Standards);
        }

        public ActionResult Create(int activityID)
        {
            ViewBag.ActivityID = activityID;
            return View();
        }
        [HttpPost]
        public ActionResult Create(PessModel.Standard standard)
        {
            ViewBag.ActivityID = standard.ActivityID;
            if (ModelState.IsValid)
            {
                db.Standards.Add(standard);
                db.SaveChanges();
                return RedirectToAction("Index", new { activityID = standard.ActivityID });
            }
            else
            {
                return View(standard);
            }
        }
        public ActionResult Edit(int id)
        {
            Standard standard = db.Standards.Find(id);
            if (standard == null)
            {
                return HttpNotFound();
            }            
            return View(standard);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles ="Admin")]
        public ActionResult Edit(Standard standard)
        {
            if (ModelState.IsValid&&standard.ActivityID!=null)
            {                
                db.Entry(standard).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();                
                return RedirectToAction("Index", "Standard", new { ActivityID = standard.ActivityID });
            }
            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", standard.ActivityID);
            return View(standard);
        }
        [Authorize(Roles ="Admin")]
        public ActionResult Delete(int id = 0)
        {
            Standard standard = db.Standards.Find(id);
            if (standard == null)
            {
                return HttpNotFound();
            }
            return View(standard);
        }

        //
        // POST: /Subject/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Standard standard = db.Standards.Find(id);
            if (standard.Responses.Count > 0)
            {
                ModelState.AddModelError("", "已经有评委根据这条规则录入了评语，故无法删除该规则！请先删除评语。");
                return View(standard);
            }
            db.Standards.Remove(standard);
            db.SaveChanges();
            return RedirectToAction("Index","Standard",new { activityID=standard.ActivityID});
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}